home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_operator.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  17KB  |  498 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import operator
  5. import unittest
  6. from test import test_support
  7.  
  8. class OperatorTestCase(unittest.TestCase):
  9.     
  10.     def test_lt(self):
  11.         self.failUnlessRaises(TypeError, operator.lt)
  12.         self.failUnlessRaises(TypeError, operator.lt, (0.0+1.0j), (0.0+2.0j))
  13.         self.failIf(operator.lt(1, 0))
  14.         self.failIf(operator.lt(1, 0.0))
  15.         self.failIf(operator.lt(1, 1))
  16.         self.failIf(operator.lt(1, 1.0))
  17.         self.failUnless(operator.lt(1, 2))
  18.         self.failUnless(operator.lt(1, 2.0))
  19.  
  20.     
  21.     def test_le(self):
  22.         self.failUnlessRaises(TypeError, operator.le)
  23.         self.failUnlessRaises(TypeError, operator.le, (0.0+1.0j), (0.0+2.0j))
  24.         self.failIf(operator.le(1, 0))
  25.         self.failIf(operator.le(1, 0.0))
  26.         self.failUnless(operator.le(1, 1))
  27.         self.failUnless(operator.le(1, 1.0))
  28.         self.failUnless(operator.le(1, 2))
  29.         self.failUnless(operator.le(1, 2.0))
  30.  
  31.     
  32.     def test_eq(self):
  33.         
  34.         class C(object):
  35.             
  36.             def __eq__(self, other):
  37.                 raise SyntaxError
  38.  
  39.  
  40.         self.failUnlessRaises(TypeError, operator.eq)
  41.         self.failUnlessRaises(SyntaxError, operator.eq, C(), C())
  42.         self.failIf(operator.eq(1, 0))
  43.         self.failIf(operator.eq(1, 0.0))
  44.         self.failUnless(operator.eq(1, 1))
  45.         self.failUnless(operator.eq(1, 1.0))
  46.         self.failIf(operator.eq(1, 2))
  47.         self.failIf(operator.eq(1, 2.0))
  48.  
  49.     
  50.     def test_ne(self):
  51.         
  52.         class C(object):
  53.             
  54.             def __ne__(self, other):
  55.                 raise SyntaxError
  56.  
  57.  
  58.         self.failUnlessRaises(TypeError, operator.ne)
  59.         self.failUnlessRaises(SyntaxError, operator.ne, C(), C())
  60.         self.failUnless(operator.ne(1, 0))
  61.         self.failUnless(operator.ne(1, 0.0))
  62.         self.failIf(operator.ne(1, 1))
  63.         self.failIf(operator.ne(1, 1.0))
  64.         self.failUnless(operator.ne(1, 2))
  65.         self.failUnless(operator.ne(1, 2.0))
  66.  
  67.     
  68.     def test_ge(self):
  69.         self.failUnlessRaises(TypeError, operator.ge)
  70.         self.failUnlessRaises(TypeError, operator.ge, (0.0+1.0j), (0.0+2.0j))
  71.         self.failUnless(operator.ge(1, 0))
  72.         self.failUnless(operator.ge(1, 0.0))
  73.         self.failUnless(operator.ge(1, 1))
  74.         self.failUnless(operator.ge(1, 1.0))
  75.         self.failIf(operator.ge(1, 2))
  76.         self.failIf(operator.ge(1, 2.0))
  77.  
  78.     
  79.     def test_gt(self):
  80.         self.failUnlessRaises(TypeError, operator.gt)
  81.         self.failUnlessRaises(TypeError, operator.gt, (0.0+1.0j), (0.0+2.0j))
  82.         self.failUnless(operator.gt(1, 0))
  83.         self.failUnless(operator.gt(1, 0.0))
  84.         self.failIf(operator.gt(1, 1))
  85.         self.failIf(operator.gt(1, 1.0))
  86.         self.failIf(operator.gt(1, 2))
  87.         self.failIf(operator.gt(1, 2.0))
  88.  
  89.     
  90.     def test_abs(self):
  91.         self.failUnlessRaises(TypeError, operator.abs)
  92.         self.failUnlessRaises(TypeError, operator.abs, None)
  93.         self.failUnless(operator.abs(-1) == 1)
  94.         self.failUnless(operator.abs(1) == 1)
  95.  
  96.     
  97.     def test_add(self):
  98.         self.failUnlessRaises(TypeError, operator.add)
  99.         self.failUnlessRaises(TypeError, operator.add, None, None)
  100.         self.failUnless(operator.add(3, 4) == 7)
  101.  
  102.     
  103.     def test_bitwise_and(self):
  104.         self.failUnlessRaises(TypeError, operator.and_)
  105.         self.failUnlessRaises(TypeError, operator.and_, None, None)
  106.         self.failUnless(operator.and_(15, 10) == 10)
  107.  
  108.     
  109.     def test_concat(self):
  110.         self.failUnlessRaises(TypeError, operator.concat)
  111.         self.failUnlessRaises(TypeError, operator.concat, None, None)
  112.         self.failUnless(operator.concat('py', 'thon') == 'python')
  113.         self.failUnless(operator.concat([
  114.             1,
  115.             2], [
  116.             3,
  117.             4]) == [
  118.             1,
  119.             2,
  120.             3,
  121.             4])
  122.  
  123.     
  124.     def test_countOf(self):
  125.         self.failUnlessRaises(TypeError, operator.countOf)
  126.         self.failUnlessRaises(TypeError, operator.countOf, None, None)
  127.         self.failUnless(operator.countOf([
  128.             1,
  129.             2,
  130.             1,
  131.             3,
  132.             1,
  133.             4], 3) == 1)
  134.         self.failUnless(operator.countOf([
  135.             1,
  136.             2,
  137.             1,
  138.             3,
  139.             1,
  140.             4], 5) == 0)
  141.  
  142.     
  143.     def test_delitem(self):
  144.         a = [
  145.             4,
  146.             3,
  147.             2,
  148.             1]
  149.         self.failUnlessRaises(TypeError, operator.delitem, a)
  150.         self.failUnlessRaises(TypeError, operator.delitem, a, None)
  151.         self.failUnless(operator.delitem(a, 1) is None)
  152.         self.assert_(a == [
  153.             4,
  154.             2,
  155.             1])
  156.  
  157.     
  158.     def test_delslice(self):
  159.         a = range(10)
  160.         self.failUnlessRaises(TypeError, operator.delslice, a)
  161.         self.failUnlessRaises(TypeError, operator.delslice, a, None, None)
  162.         self.failUnless(operator.delslice(a, 2, 8) is None)
  163.         self.assert_(a == [
  164.             0,
  165.             1,
  166.             8,
  167.             9])
  168.  
  169.     
  170.     def test_div(self):
  171.         self.failUnlessRaises(TypeError, operator.div, 5)
  172.         self.failUnlessRaises(TypeError, operator.div, None, None)
  173.         self.failUnless(operator.floordiv(5, 2) == 2)
  174.  
  175.     
  176.     def test_floordiv(self):
  177.         self.failUnlessRaises(TypeError, operator.floordiv, 5)
  178.         self.failUnlessRaises(TypeError, operator.floordiv, None, None)
  179.         self.failUnless(operator.floordiv(5, 2) == 2)
  180.  
  181.     
  182.     def test_truediv(self):
  183.         self.failUnlessRaises(TypeError, operator.truediv, 5)
  184.         self.failUnlessRaises(TypeError, operator.truediv, None, None)
  185.         self.failUnless(operator.truediv(5, 2) == 2.5)
  186.  
  187.     
  188.     def test_getitem(self):
  189.         a = range(10)
  190.         self.failUnlessRaises(TypeError, operator.getitem)
  191.         self.failUnlessRaises(TypeError, operator.getitem, a, None)
  192.         self.failUnless(operator.getitem(a, 2) == 2)
  193.  
  194.     
  195.     def test_getslice(self):
  196.         a = range(10)
  197.         self.failUnlessRaises(TypeError, operator.getslice)
  198.         self.failUnlessRaises(TypeError, operator.getslice, a, None, None)
  199.         self.failUnless(operator.getslice(a, 4, 6) == [
  200.             4,
  201.             5])
  202.  
  203.     
  204.     def test_indexOf(self):
  205.         self.failUnlessRaises(TypeError, operator.indexOf)
  206.         self.failUnlessRaises(TypeError, operator.indexOf, None, None)
  207.         self.failUnless(operator.indexOf([
  208.             4,
  209.             3,
  210.             2,
  211.             1], 3) == 1)
  212.         self.assertRaises(ValueError, operator.indexOf, [
  213.             4,
  214.             3,
  215.             2,
  216.             1], 0)
  217.  
  218.     
  219.     def test_invert(self):
  220.         self.failUnlessRaises(TypeError, operator.invert)
  221.         self.failUnlessRaises(TypeError, operator.invert, None)
  222.         self.failUnless(operator.inv(4) == -5)
  223.  
  224.     
  225.     def test_isCallable(self):
  226.         self.failUnlessRaises(TypeError, operator.isCallable)
  227.         
  228.         class C:
  229.             pass
  230.  
  231.         
  232.         def check(self, o, v):
  233.             None(self.assert_ if callable(o) == callable(o) else callable(o) == v)
  234.  
  235.         check(self, 4, 0)
  236.         check(self, operator.isCallable, 1)
  237.         check(self, C, 1)
  238.         check(self, C(), 0)
  239.  
  240.     
  241.     def test_isMappingType(self):
  242.         self.failUnlessRaises(TypeError, operator.isMappingType)
  243.         self.failIf(operator.isMappingType(1))
  244.         self.failIf(operator.isMappingType(operator.isMappingType))
  245.         self.failUnless(operator.isMappingType(operator.__dict__))
  246.         self.failUnless(operator.isMappingType({ }))
  247.  
  248.     
  249.     def test_isNumberType(self):
  250.         self.failUnlessRaises(TypeError, operator.isNumberType)
  251.         self.failUnless(operator.isNumberType(8))
  252.         self.failUnless(operator.isNumberType((0.0+8.0j)))
  253.         self.failUnless(operator.isNumberType(0x8L))
  254.         self.failUnless(operator.isNumberType(8.3000000000000007))
  255.         self.failIf(operator.isNumberType(dir()))
  256.  
  257.     
  258.     def test_isSequenceType(self):
  259.         self.failUnlessRaises(TypeError, operator.isSequenceType)
  260.         self.failUnless(operator.isSequenceType(dir()))
  261.         self.failUnless(operator.isSequenceType(()))
  262.         self.failUnless(operator.isSequenceType(xrange(10)))
  263.         self.failUnless(operator.isSequenceType('yeahbuddy'))
  264.         self.failIf(operator.isSequenceType(3))
  265.  
  266.     
  267.     def test_lshift(self):
  268.         self.failUnlessRaises(TypeError, operator.lshift)
  269.         self.failUnlessRaises(TypeError, operator.lshift, None, 42)
  270.         self.failUnless(operator.lshift(5, 1) == 10)
  271.         self.failUnless(operator.lshift(5, 0) == 5)
  272.         self.assertRaises(ValueError, operator.lshift, 2, -1)
  273.  
  274.     
  275.     def test_mod(self):
  276.         self.failUnlessRaises(TypeError, operator.mod)
  277.         self.failUnlessRaises(TypeError, operator.mod, None, 42)
  278.         self.failUnless(operator.mod(5, 2) == 1)
  279.  
  280.     
  281.     def test_mul(self):
  282.         self.failUnlessRaises(TypeError, operator.mul)
  283.         self.failUnlessRaises(TypeError, operator.mul, None, None)
  284.         self.failUnless(operator.mul(5, 2) == 10)
  285.  
  286.     
  287.     def test_neg(self):
  288.         self.failUnlessRaises(TypeError, operator.neg)
  289.         self.failUnlessRaises(TypeError, operator.neg, None)
  290.         self.failUnless(operator.neg(5) == -5)
  291.         self.failUnless(operator.neg(-5) == 5)
  292.         self.failUnless(operator.neg(0) == 0)
  293.         self.failUnless(operator.neg(-0) == 0)
  294.  
  295.     
  296.     def test_bitwise_or(self):
  297.         self.failUnlessRaises(TypeError, operator.or_)
  298.         self.failUnlessRaises(TypeError, operator.or_, None, None)
  299.         self.failUnless(operator.or_(10, 5) == 15)
  300.  
  301.     
  302.     def test_pos(self):
  303.         self.failUnlessRaises(TypeError, operator.pos)
  304.         self.failUnlessRaises(TypeError, operator.pos, None)
  305.         self.failUnless(operator.pos(5) == 5)
  306.         self.failUnless(operator.pos(-5) == -5)
  307.         self.failUnless(operator.pos(0) == 0)
  308.         self.failUnless(operator.pos(-0) == 0)
  309.  
  310.     
  311.     def test_pow(self):
  312.         self.failUnlessRaises(TypeError, operator.pow)
  313.         self.failUnlessRaises(TypeError, operator.pow, None, None)
  314.         self.failUnless(operator.pow(3, 5) == 3 ** 5)
  315.         self.failUnless(operator.__pow__(3, 5) == 3 ** 5)
  316.         self.assertRaises(TypeError, operator.pow, 1)
  317.         self.assertRaises(TypeError, operator.pow, 1, 2, 3)
  318.  
  319.     
  320.     def test_repeat(self):
  321.         a = range(3)
  322.         self.failUnlessRaises(TypeError, operator.repeat)
  323.         self.failUnlessRaises(TypeError, operator.repeat, a, None)
  324.         self.failUnless(operator.repeat(a, 2) == a + a)
  325.         self.failUnless(operator.repeat(a, 1) == a)
  326.         self.failUnless(operator.repeat(a, 0) == [])
  327.         a = (1, 2, 3)
  328.         self.failUnless(operator.repeat(a, 2) == a + a)
  329.         self.failUnless(operator.repeat(a, 1) == a)
  330.         self.failUnless(operator.repeat(a, 0) == ())
  331.         a = '123'
  332.         self.failUnless(operator.repeat(a, 2) == a + a)
  333.         self.failUnless(operator.repeat(a, 1) == a)
  334.         self.failUnless(operator.repeat(a, 0) == '')
  335.  
  336.     
  337.     def test_rshift(self):
  338.         self.failUnlessRaises(TypeError, operator.rshift)
  339.         self.failUnlessRaises(TypeError, operator.rshift, None, 42)
  340.         self.failUnless(operator.rshift(5, 1) == 2)
  341.         self.failUnless(operator.rshift(5, 0) == 5)
  342.         self.assertRaises(ValueError, operator.rshift, 2, -1)
  343.  
  344.     
  345.     def test_contains(self):
  346.         self.failUnlessRaises(TypeError, operator.contains)
  347.         self.failUnlessRaises(TypeError, operator.contains, None, None)
  348.         self.failUnless(operator.contains(range(4), 2))
  349.         self.failIf(operator.contains(range(4), 5))
  350.         self.failUnless(operator.sequenceIncludes(range(4), 2))
  351.         self.failIf(operator.sequenceIncludes(range(4), 5))
  352.  
  353.     
  354.     def test_setitem(self):
  355.         a = range(3)
  356.         self.failUnlessRaises(TypeError, operator.setitem, a)
  357.         self.failUnlessRaises(TypeError, operator.setitem, a, None, None)
  358.         self.failUnless(operator.setitem(a, 0, 2) is None)
  359.         self.assert_(a == [
  360.             2,
  361.             1,
  362.             2])
  363.         self.assertRaises(IndexError, operator.setitem, a, 4, 2)
  364.  
  365.     
  366.     def test_setslice(self):
  367.         a = range(4)
  368.         self.failUnlessRaises(TypeError, operator.setslice, a)
  369.         self.failUnlessRaises(TypeError, operator.setslice, a, None, None, None)
  370.         self.failUnless(operator.setslice(a, 1, 3, [
  371.             2,
  372.             1]) is None)
  373.         self.assert_(a == [
  374.             0,
  375.             2,
  376.             1,
  377.             3])
  378.  
  379.     
  380.     def test_sub(self):
  381.         self.failUnlessRaises(TypeError, operator.sub)
  382.         self.failUnlessRaises(TypeError, operator.sub, None, None)
  383.         self.failUnless(operator.sub(5, 2) == 3)
  384.  
  385.     
  386.     def test_truth(self):
  387.         
  388.         class C(object):
  389.             
  390.             def __nonzero__(self):
  391.                 raise SyntaxError
  392.  
  393.  
  394.         self.failUnlessRaises(TypeError, operator.truth)
  395.         self.failUnlessRaises(SyntaxError, operator.truth, C())
  396.         self.failUnless(operator.truth(5))
  397.         self.failUnless(operator.truth([
  398.             0]))
  399.         self.failIf(operator.truth(0))
  400.         self.failIf(operator.truth([]))
  401.  
  402.     
  403.     def test_bitwise_xor(self):
  404.         self.failUnlessRaises(TypeError, operator.xor)
  405.         self.failUnlessRaises(TypeError, operator.xor, None, None)
  406.         self.failUnless(operator.xor(11, 12) == 7)
  407.  
  408.     
  409.     def test_is(self):
  410.         a = b = 'xyzpdq'
  411.         c = a[:3] + b[3:]
  412.         self.failUnlessRaises(TypeError, operator.is_)
  413.         self.failUnless(operator.is_(a, b))
  414.         self.failIf(operator.is_(a, c))
  415.  
  416.     
  417.     def test_is_not(self):
  418.         a = b = 'xyzpdq'
  419.         c = a[:3] + b[3:]
  420.         self.failUnlessRaises(TypeError, operator.is_not)
  421.         self.failIf(operator.is_not(a, b))
  422.         self.failUnless(operator.is_not(a, c))
  423.  
  424.     
  425.     def test_attrgetter(self):
  426.         
  427.         class A:
  428.             pass
  429.  
  430.         a = A()
  431.         a.name = 'arthur'
  432.         f = operator.attrgetter('name')
  433.         self.assertEqual(f(a), 'arthur')
  434.         f = operator.attrgetter('rank')
  435.         self.assertRaises(AttributeError, f, a)
  436.         f = operator.attrgetter(2)
  437.         self.assertRaises(TypeError, f, a)
  438.         self.assertRaises(TypeError, operator.attrgetter)
  439.         self.assertRaises(TypeError, operator.attrgetter, 1, 2)
  440.         
  441.         class C(object):
  442.             
  443.             def __getattr(self, name):
  444.                 raise SyntaxError
  445.  
  446.  
  447.         self.failUnlessRaises(AttributeError, operator.attrgetter('foo'), C())
  448.  
  449.     
  450.     def test_itemgetter(self):
  451.         a = 'ABCDE'
  452.         f = operator.itemgetter(2)
  453.         self.assertEqual(f(a), 'C')
  454.         f = operator.itemgetter(10)
  455.         self.assertRaises(IndexError, f, a)
  456.         
  457.         class C(object):
  458.             
  459.             def __getitem(self, name):
  460.                 raise SyntaxError
  461.  
  462.  
  463.         self.failUnlessRaises(TypeError, operator.itemgetter(42), C())
  464.         f = operator.itemgetter('name')
  465.         self.assertRaises(TypeError, f, a)
  466.         self.assertRaises(TypeError, operator.itemgetter)
  467.         self.assertRaises(TypeError, operator.itemgetter, 1, 2)
  468.         d = dict(key = 'val')
  469.         f = operator.itemgetter('key')
  470.         self.assertEqual(f(d), 'val')
  471.         f = operator.itemgetter('nonkey')
  472.         self.assertRaises(KeyError, f, d)
  473.         inventory = [
  474.             ('apple', 3),
  475.             ('banana', 2),
  476.             ('pear', 5),
  477.             ('orange', 1)]
  478.         getcount = operator.itemgetter(1)
  479.         self.assertEqual(map(getcount, inventory), [
  480.             3,
  481.             2,
  482.             5,
  483.             1])
  484.         self.assertEqual(sorted(inventory, key = getcount), [
  485.             ('orange', 1),
  486.             ('banana', 2),
  487.             ('apple', 3),
  488.             ('pear', 5)])
  489.  
  490.  
  491.  
  492. def test_main():
  493.     test_support.run_unittest(OperatorTestCase)
  494.  
  495. if __name__ == '__main__':
  496.     test_main()
  497.  
  498.